<?php defined('SYSPATH') OR die('No direct access allowed.');

class Contcategory_Model extends ORM_Tree{

	public $parent_column='collection_id';
	
	protected $belongs_to=array('collection');
	protected $has_and_belongs_to_many=array('contents');	
	protected $sorting = array('title' => 'asc');
 
 
	public function before_delete(){
	
	}

	public function title_allready_exists($title, $type){
		$all=ORM::factory('contcategory')->where(array('title'=>$title, 'type'=>$type))->find_all();
		if($all->count()>0){
		return true;
		}
		return false;
	}

	public function get_pulldown_list($firstpoints=NULL){
		
		$all=ORM::factory('contcategory')->orderby(array('collection_id'=>'', 'position'=>''))->find_all()->as_array();
		$list=array();
		if($firstpoints){
			$list+=$firstpoints;
		}
		
		$list+=$this->get_nested_pulldown_list($all,'0', '1');
		
		return $list;
	}
	
	public function get_nested_pulldown_list ($all, $parent_id, $step){
		$array=array();
		foreach($all as $onecat){			
			if($onecat->parent_id==$parent_id){
				$str='';
				for($i=0;$i<$step;$i++){
					$str.='&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				if($onecat->id!=$this->id){
					$array[$onecat->id]=$str.$onecat->title;
					$array+=$this->get_nested_pulldown_list($categories, $onecat->id, $step+1);			
				}
			}			
		}
		return $array;
	}


}



